package fun.rockchen.controller;

import fun.rockchen.anno.SystemControllerLog;
import fun.rockchen.anno.TokenValidate;
import fun.rockchen.bean.AddTaskEntity;
import fun.rockchen.bean.MapTaskEntity;
import fun.rockchen.bean.Task;
import fun.rockchen.bean.TaskTimeEntity;
import fun.rockchen.service.interfaceService.TaskService;
import fun.rockchen.utils.responseUtils.ResponseJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @author Rock Chen
 * @Description:
 * @date 2023/10/16 11:37
 */

@SystemControllerLog(operation = "任务模块", type = "info")
@RequestMapping("/api/task")
@RestController
public class TaskController {
    private final TaskService taskService;

    @Autowired
    public TaskController(TaskService taskService) {
        this.taskService = taskService;
    }

    @TokenValidate(operation = "--------token校验--------")
    @PostMapping
    public ResponseEntity<?> addTask(@RequestBody AddTaskEntity addTaskEntity) {
        String startTime = addTaskEntity.getStartTime();
        String endTime = addTaskEntity.getEndTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date startDate = format.parse(startTime);
            Date endDate = format.parse(endTime);
            long s1 = startDate.getTime();
            long e1 = endDate.getTime();
            long diff = e1 - s1;
            // startTime早与endTime
            if (diff < 0) {
                return ResponseEntity.ok().body(ResponseJson.startTimeEarly());
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return taskService.addTask(addTaskEntity);
    }

    @TokenValidate(operation = "--------token校验--------")
    @DeleteMapping()
    public ResponseEntity<?> deleteTask(@RequestParam String taskId) {
        return taskService.deleteTask(taskId);
    }

    @TokenValidate(operation = "--------token校验--------")
    @PutMapping
    public ResponseEntity<?> updateTask(@RequestBody Task task) {
        String startTime = task.getStartTime();
        String endTime = task.getEndTime();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date startDate = format.parse(startTime);
            Date endDate = format.parse(endTime);
            long s1 = startDate.getTime();
            long e1 = endDate.getTime();
            long diff = e1 - s1;
            // startTime早与endTime
            if (diff < 0) {
                return ResponseEntity.ok().body(ResponseJson.startTimeEarly());
            }
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return taskService.updateTask(task);
    }

    @TokenValidate(operation = "--------token校验--------")
    @PutMapping("/changeCancel")
    public ResponseEntity<?> changeTaskIntoCancel(@RequestBody MapTaskEntity mapTaskEntity) {
        return taskService.changeTaskIntoCancel(mapTaskEntity);
    }

    @TokenValidate(operation = "--------token校验--------")
    @PutMapping("/changePending")
    public ResponseEntity<?> changeTaskIntoPending(@RequestBody MapTaskEntity mapTaskEntity) {
        return taskService.changeTaskIntoPending(mapTaskEntity);
    }

    @TokenValidate(operation = "--------token校验--------")
    @GetMapping("/statusTask")
    public ResponseEntity<?> getStatusTask(@ModelAttribute MapTaskEntity mapTaskEntity) {
        return taskService.getStatusTask(mapTaskEntity);
    }

    @TokenValidate(operation = "--------token校验--------")
    @GetMapping("/todayTask")
    public ResponseEntity<?> getTodayTask(@ModelAttribute MapTaskEntity mapTaskEntity) {
        return taskService.getTodayTask(mapTaskEntity);
    }

    @TokenValidate(operation = "--------token校验--------")
    @GetMapping("/taskByTime")
    public ResponseEntity<?> getTaskByTime(@ModelAttribute MapTaskEntity mapTaskEntity) throws ParseException {
        return taskService.getTaskByTime(mapTaskEntity);
    }

    @TokenValidate(operation = "--------token校验--------")
    @GetMapping("/taskByStatus")
    public ResponseEntity<?> getTaskByState(@ModelAttribute MapTaskEntity mapTaskEntity) {
        return taskService.getTaskByState(mapTaskEntity);
    }

    @TokenValidate(operation = "--------token校验--------")
    @GetMapping("/taskId")
    public ResponseEntity<?> getTaskById(@RequestParam String id) {
        return taskService.getTaskById(id);
    }

    @TokenValidate(operation = "--------token校验--------")
    @GetMapping("/taskByDate")
    public ResponseEntity<?> getTaskByDate(@ModelAttribute MapTaskEntity mapTaskEntity) {
        return taskService.getTaskByDate(mapTaskEntity);
    }

    @TokenValidate(operation = "--------token校验--------")
    @GetMapping("/taskTotalTime")
    public ResponseEntity<?> getTaskTotalTime(@ModelAttribute TaskTimeEntity taskTimeEntity) {
        return taskService.getTaskTotalTime(taskTimeEntity);
    }
}
